/*
 * Edit Video Module JS
 * This creates the admin GUI Edit Page for the Unit Video Module
 *
 * JSLint.com Check: 
 */

RM.Pages.Functions.Video_Unit_Save = function(){
    RM.Pages.Video_Unit_Edit_Form.getForm().submit({
        success: function(form, action) {
//            // update the status bar...
//            RM.Pages.Units_StatusBar = Ext.getCmp('rm_pages_video_edit_statusbar');
//            RM.Pages.Units_StatusBar.setStatus({
//                text: RM.Translate.Admin.Video.Edit.Saved,
//                iconCls: 'ok-icon'
//            });
        },
        waitMsg: RM.Translate.Common.Saving,
        waitTitle: RM.Translate.Common.PleaseWait
    });
};

RM.Pages.Video_Unit_Toolbar = {
    xtype : "panel",
    id : "rm_pages_video_unit_toolbar",
    bodyBorder : false,
    html : RM.Common.getToolbar([{image: RM.BaseLargeImageURL+"save.gif", label: RM.Translate.Common.Save, link: "RM.Pages.Functions.Video_Unit_Save()"}])
};
RM.Toolbars.push(RM.Pages.Video_Unit_Toolbar);

RM.Pages.Video_Feed_Fieldset = new Ext.form.FieldSet({
    id: "rm_pages_video_feed_fieldset",
    title: RM.Translate.Admin.Video.Feeds.Title,
    autoHeight: true,
    layout: 'form',
    bodyBorder : false,
    defaults: {width: 300},
    labelWidth: 190,
    items: [
            {
                xtype: 'textarea',
                fieldLabel: RM.Translate.Admin.Video.Feeds.Embed,
                id: 'rm_video_edit[embedcode]',
                allowBlank:true
            },{
                fieldLabel: RM.Translate.Admin.Video.Feeds.Provider,
                id : "rm_video_edit[provider_id]",
                name : "rm_video_edit[provider_name]",
                hiddenName : "rm_video_edit[provider]",
                xtype: "combo",
                mode: 'local',
                triggerAction: 'all',
                typeAhead: true,
                resizable: true,
                width: 180,
                store: [
                    ['youtube', RM.Translate.Admin.Video.Providers.YouTube],
                    ['vimeo', RM.Translate.Admin.Video.Providers.Vimeo],
                    ['dailymotion', RM.Translate.Admin.Video.Providers.DailyMotion]
                ]
            }
    ]
});

// load the values
RM.Pages.Functions.Video_Unit_EditJson_Request = function(){

    var unit_id = Ext.getCmp('edit_unit[id]').getValue();

    var myMask = new Ext.LoadMask('content-panel', {msg:RM.Translate.Common.PleaseWait});
    myMask.show();

    var conn = new Ext.data.Connection();
    var request = {
        url: RM.Common.AssembleURL({
            controller : 'Video',
            action: 'loadJson',
            parameters : [{
                name : 'id',
                value : unit_id
            }]
        }),
        method: 'POST',
        success: function(responseObject) {
            eval('RM.Pages.Functions.Video_Unit_ConfigJson('+responseObject.responseText+');');
            myMask.hide();
        },
        failure: function() {
            Ext.Msg.alert(RM.Translate.Common.Status, RM.Translate.Common.UnableToShow);
            myMask.hide();
        }
    };
    conn.request(request);
};

// clear the old form values
RM.Pages.Functions.Video_Unit_ClearForm = function (){
    Ext.getCmp('rm_video_edit[unit_id]').setValue("");
    Ext.getCmp('rm_video_edit[feed_id]').setValue("");
};

RM.Pages.Functions.Video_Unit_ConfigJson = function (responseObject) {

    RM.Pages.Functions.Video_Unit_ClearForm();

    if (responseObject[0]!=undefined){
        Ext.getCmp('rm_video_edit[provider_id]').setValue(responseObject[0]['provider']);
        Ext.getCmp('rm_video_edit[embedcode]').setValue(responseObject[0]['embedcode']);
        Ext.getCmp('rm_video_edit[feed_id]').setValue(responseObject[0]['feed_id']);
    }
    Ext.getCmp('rm_video_edit[unit_id]').setValue(Ext.getCmp('edit_unit[id]').getValue());

    Ext.getCmp("rm_pages_video_unit_edit_form").doLayout(); // force the layout
};

RM.Pages.Video_Unit_Edit_Form = new Ext.FormPanel({
    id : "rm_pages_video_unit_edit_form",
    title: RM.Translate.Admin.Video.Main.Title,
    xtype : "form",
    height: 600,
    autoScroll: true,
    url : RM.Common.AssembleURL({
        controller: 'Video',
        action: 'updateJson'
    }),
    frame : true,
    items : [
        {
            xtype: "hidden",
            id: 'rm_video_edit[unit_id]'
        },{
            xtype: "hidden",
            id: 'rm_video_edit[feed_id]'
        },
        RM.Pages.Video_Feed_Fieldset,
    ],
    listeners: {
        'beforehide' : function(){
            Ext.getCmp('toolbar_panel').layout.setActiveItem('rm_pages_units_edit_toolbar');
            return true;
        },
        'beforeshow' : function(){
            Ext.getCmp('toolbar_panel').layout.setActiveItem('rm_pages_video_unit_toolbar');
            RM.Pages.Functions.Video_Unit_EditJson_Request();
            return true;
        }
    }
});

RM.Pages.Units_Edit_TabPanel.add(RM.Pages.Video_Unit_Edit_Form);
